Viswanath
It's My Show Case
* Home
* Profile
* Design Zone
* Coding Zone
* RSS
Using lex and yacc tools on windows
Recently I came accross a situation where I have to use Lex tools for
writing lexical analyzers. Generally these tools are avilable on most of
the standard UNIX based operating sustems. But I use Windows Operating
system. So, I just wanted to share my experience of installing them,
writing sample programs and compiling them and executing them.
* *
*Let me first start with how to install them on your windows machine...*
(1) Download Flex
(Free implementations of LEX library), Bison
(Free
implementation of Bison library) and Dev-CPP
(2) Install them on your machine (Note: *without spaces in the name of
the directory*)
(3)Set PATH variable to include bin directories of Flex, Bison, gcc
*Now compiling .l and .y files and generating .exe file... *
*||*Open your command prompt and go to directory where you lex and yacc
files are located and run below commands
(1) flex filename.l
===> it generates *lex.yy.c* file
(2) bison filename.y
===> it generates *y.tab.c *and *y.tab.h* file
(3) gcc lex.yy.c y.tab.c -o test.exe
===> it generates executable file (.exe)
*Note: *You can run gcc command only with lex.yy.c file also. ex: gcc
lex.yy.c -o .exe
*
Sample lex file (whitespace.l):*
* *
non_white [^ \t\n]*
%%
{non_white} ECHO;
. ;
[\n] ;
%%
int yywrap(void)
{
return 0;
}
int main(void)
{
yylex();
return 0;
}
*
Sample lex file (lexicalAnalyzer.l):*
* *
DIGIT [0-9]
ID [a-z][a-z0-9]*
%%
char|int|float|double|main|void|real|printf|for|while|do printf("keyword=%s\n",yytext);
{DIGIT} printf("integer %s\n",yytext);
{ID}+ printf("identifier %s\n",yytext);
{DIGIT}+"."{DIGIT}* printf("floating point number:%s\n",yytext);
[=|!=|>=|<=|<|>|+|-|*|/] printf("operator %s\n",yytext);
[(] printf("openparenthesis %s\n",yytext);
[)] printf("closeparenthesis %s\n",yytext);
[{] printf("open bbrace is %s\n",yytext);
[}] printf("close braceis %s\n",yytext);
[;] printf("eos %s\n",yytext);
[\t\n]+ /*eat up whitesapaces */
["{"|^}\n]*"}"] /*eat up comments */
["|,|.] printf("punctaation :%s \n",yytext);
%%
main()
{
yylex();
}
int yywrap()
{
return 1;
}
3:33 AM | Category: | 0 comments
Comments (0)
Post a Comment
Older Post
Home
Blog Archive
* ▼ 2012
(2)
o ▼ September
(1)
+ Using lex and yacc tools on windows
o ► January
(1)
* ► 2011
(1)
o ► November
(1)
* ► 2010
(18)
o ► May
(7)
o ► April
(4)
o ► January
(7)
* ► 2009
(6)
o ► December
(3)
o ► November
(3)
/www.blogger.com/rearrange?blogID=5490094840375607687&widgetType=BlogArchive&widgetId=BlogArchive1&action=editWidget§ionId=sidebar1>
rss
© Copyright 2009. All right reserved.